package jp.yvn.game.shooting.logic;

import android.graphics.PointF;

/**
 * 雑魚敵の行動パターンクラスです
 * 
 * @author Yvn_pq
 */
public class EnemyBehaviorPattern {

	/** 直線 */
	public static final int LINE = 0;

	/** 放物線(上から上) */
	public static final int PARABOLA_FROM_TOP_TO_TOP = 1;

	/** 重力加速度 */
	public static final float mGravity = 40.0f;

	/** 開始点x */
	private float mStartX = 0;

	/** 開始点y */
	private float mStartY = 0;

	/** 終点x */
	private float mEndX = 0;

	/** 終点y */
	private float mEndY = 0;

	/** x軸速さ(pixel/flame) */
	private float mXSpeed = 0.0f;

	/** y軸速さ(pixel/flame) */
	private float mYSpeed = 0.0f;

	/** 移動時間 */
	private float mTime = 0.0f;

	/** 移動開始時間 */
	private long mStartTime = 0;

	double passageSeconds = 0;

	public EnemyBehaviorPattern(PointF startPoint, PointF endPoint, float mTime, int behaviorPattern) {
		this.mStartX = startPoint.x;
		this.mStartY = startPoint.y;
		this.mEndX = endPoint.x;
		this.mEndY = endPoint.y;
		this.mTime = mTime;
		mStartTime = System.currentTimeMillis();
		switch (behaviorPattern) {
		case LINE:
			initLine();
			break;
		case PARABOLA_FROM_TOP_TO_TOP:
			initParabolaFromTopToTop();
			break;
		}
	}

	public float getSpeedX() {
		return mXSpeed;
	}

	/**
	 * 直線用初期化
	 */
	private void initLine() {
		float dx = mEndX - mStartX;
		mXSpeed = dx / mTime;
		float dy = mEndY - mStartY;
		mYSpeed = dy / mTime;
	}

	/**
	 * 放物線用初期化
	 */
	private void initParabolaFromTopToTop() {
		float dx = mEndX - mStartX;
		mXSpeed = dx / mTime;
		mYSpeed = mGravity * (mTime / 2); // 最初のy方向速度
	}

	public void getNextPoint(PointF p, int behaviorPattern) {
		switch (behaviorPattern) {
		case LINE:
			getPointLine(p);
			break;
		case PARABOLA_FROM_TOP_TO_TOP:
			getPointParabolaFromTopToTop(p);
			break;
		default:
			break;
		}
	}

	/**
	 * 直線の移動先座標を取得します
	 * 
	 * @param 現在x
	 * @param 現在y
	 * @return 次の座標
	 */
	private void getPointLine(PointF p) {
		passageSeconds = (System.currentTimeMillis() - mStartTime) / 1000.0;
		mStartTime = System.currentTimeMillis();
		p.x += mXSpeed * (float) passageSeconds;
		p.y += mYSpeed * (float) passageSeconds;
	}

	/**
	 * 放物線の移動先座標を取得します
	 * 
	 * @param 現在x
	 * @param 現在y
	 * @return 次の座標
	 */
	private void getPointParabolaFromTopToTop(PointF p) {
		passageSeconds = (System.currentTimeMillis() - mStartTime) / 1000.0;
		mStartTime = System.currentTimeMillis();
		mYSpeed -= mGravity * passageSeconds;
		p.x += mXSpeed * (float) passageSeconds;
		p.y += mYSpeed * (float) passageSeconds;
	}
}
